import * as actions from '../actions/index';

// 对页面prop 数据进行管理
function getInitialState(pageInfo) {
  const { pathResource, resourceSubject } = pageInfo;
  const branchName =
    resourceSubject && resourceSubject.subject === 'project_security'
      ? resourceSubject.revision
      : pathResource.defaultBranch;
  // 页面初始化数据
  const initialState = {
    branchName,
    pinelineInfo: null,
  };
  return initialState;
}

const defaultAction = {
  type: 'doNothing',
};

export default function index(state = getInitialState(window.FORCE), action = defaultAction) {
  switch (action.type) {
    case actions.UPDATE_DATA:
      return { ...state, ...action.data };
    case actions.UPDATE_PIPELINE_INFO:
      return { ...state, pinelineInfo: action.data };
    case actions.INIT_BRANCH:
      return getInitialState(window.FORCE);
    default:
      return state;
  }
}
